package com.spsoft.vatoutinv.service.flowprocess;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.spsoft.core.enums.ResponseEnum;
import com.spsoft.core.exception.BusinessException;
import com.spsoft.core.model.Result;
import com.spsoft.core.model.bo.TokenUser;
import com.spsoft.core.model.ProcessParamBo;
import com.spsoft.core.utils.SecurityUtils;
import com.spsoft.core.utils.StringUtil;
import com.spsoft.vatoutinv.model.dto.vmsfullyfmmessage.VmsFullYfmMessageDto;
import com.spsoft.vatoutinv.service.vmsenterpriseinfo.VmsEnterpriseInfoService;
import com.spsoft.vatoutinv.service.vmsfullyfmmessage.VmsFullYfmMessageService;
import com.spsoft.vms.redis.service.BswjRedisService;
import com.spsoft.workflow.common.enums.BusFlowTypeEnums;
import com.spsoft.workflow.common.enums.bus.VmsFullYfmEnums;
import com.spsoft.workflow.service.BusinessHandlerService;
import com.spsoft.workflow.service.ProcessBusinessService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 预赋码流程业务实现类，bean会自动注入
 *
 * @See BusFlowTypeEnums.FLOW_YFMLY
 * 预赋码
 * @author liwm
 *
 * @date 2023/12/22
 */
@Service("FLOW_YFMLY")
@Slf4j
@RequiredArgsConstructor
public class VmsFullYfmProcessHandlerServiceImpl implements BusinessHandlerService {

    private final BswjRedisService bswjRedisService;

    private final VmsFullYfmMessageService vmsFullYfmMessageService;

    private final VmsEnterpriseInfoService vmsEnterpriseInfoService;

    private final ProcessBusinessService processBusinessService;

    /**
     * 直连单位
     */
    @Value("${leqi.zldwId}")
    private String zldw;

    @Override
    public void processHandler(ProcessParamBo pp) throws BusinessException {
        Long busId=pp.getBusId();
        String busDataJson=pp.getBusData();
        String nodeId=pp.getNodeId();

        VmsFullYfmEnums e = VmsFullYfmEnums.valueOfCode(nodeId);
        if (e == null) {
            throw new BusinessException(ResponseEnum.PARAM_VALIDATED_ERROR.getCode(), "审批流程有误，请联系管理员。");
        }
        switch (e) {
            case ROOT:
            case FLOW_YFM_SP:
                /**
                 * root节点和审批节点数据校验
                 */
                if (StringUtil.isBlank(busDataJson)) {
                    throw new BusinessException(ResponseEnum.PARAM_VALIDATED_ERROR.getCode(), "领用数量不能为空。");
                }
                // 获取预赋码领用数量
                Long lysl = JSONObject.parseObject(busDataJson).getLong("lysl");
                if (ObjectUtil.isEmpty(lysl) || lysl <= 0 || lysl > 5000) {
                    throw new BusinessException(ResponseEnum.YFM_APPLY_COUNT_ERROR);
                }
                BusFlowTypeEnums busFlowTypeEnums = BusFlowTypeEnums.FLOW_YFMLY;
                boolean processNotFinished = processBusinessService.isProcessNotEnd(SecurityUtils.getDeptCode(), busId, busFlowTypeEnums);
                if (processNotFinished) {
                    throw new BusinessException(ResponseEnum.CREDIT_AND_YFM_PROCESS_NOT_FINISH);
                }
                break;
            case FLOW_YFM_LY:
                /**
                 * 预赋码领用
                 */
                TokenUser user = SecurityUtils.getTokenUser();
                Long lysl2 = JSONObject.parseObject(busDataJson).getLong("lysl");

                String sydwbh = bswjRedisService.getSydwbh(user.getDeptCode());
                if (StringUtil.isBlank(sydwbh)) {
                    throw new BusinessException(ResponseEnum.PTBH_NOT_FOUND);
                }

                VmsFullYfmMessageDto dto = new VmsFullYfmMessageDto();
                dto.setLysl(lysl2);
                dto.setNsrsbh(vmsEnterpriseInfoService.getNsrsbh());
                dto.setYwlsh(sydwbh + zldw + RandomUtil.randomString(32));
                dto.setDeptCode(user.getDeptCode());
                log.info("预赋码领用调用乐企接口请求参数：{}", JSONObject.toJSONString(dto));
                Result<Object> result = vmsFullYfmMessageService.getVmsFullYfmMessageByLq(dto, user);
                if (!ResponseEnum.SUCCESS_MSG.getCode().equals(result.getCode())) {
                    throw new BusinessException(result.getCode(), result.getMessage());
                }
                break;
            default:
                // 未知节点报错
                throw new BusinessException(ResponseEnum.PROCESS_NODE_ERROR);
        }
    }
}
